<?php
/**
 * Class that operate on table 'articles'. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2015-05-26 14:36
 */
class ArticlesMySqlDAO implements ArticlesDAO{

	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return ArticlesMySql 
	 */
	public function load($id){
		$sql = 'SELECT * FROM articles WHERE article_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($id);
		return $this->getRow($sqlQuery);
	}

	/**
	 * Get all records from table
	 */
	public function queryAll(){
		$sql = 'SELECT * FROM articles';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function queryAllOrderBy($orderColumn){
		$sql = 'SELECT * FROM articles ORDER BY '.$orderColumn;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
 	 * Delete record from table
 	 * @param article primary key
 	 */
	public function delete($article_id){
		$sql = 'DELETE FROM articles WHERE article_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($article_id);
		return $this->executeUpdate($sqlQuery);
	}
	
	/**
 	 * Insert record to table
 	 *
 	 * @param ArticlesMySql article
 	 */
	public function insert($article){
		$sql = 'INSERT INTO articles (article_type_id, article_title, article_author_id, article_disclaimer_id, publish_date, expiry_date, description, status, created_date, updated_date) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->setNumber($article->articleTypeId);
		$sqlQuery->set($article->articleTitle);
		$sqlQuery->setNumber($article->articleAuthorId);
		$sqlQuery->setNumber($article->articleDisclaimerId);
		$sqlQuery->set($article->publishDate);
		$sqlQuery->set($article->expiryDate);
		$sqlQuery->set($article->description);
		$sqlQuery->setNumber($article->status);
		$sqlQuery->set($article->created_date);
		$sqlQuery->set($article->updated_date);

		$id = $this->executeInsert($sqlQuery);	
		$article->articleId = $id;
		return $id;
	}
	
	/**
 	 * Update record in table
 	 *
 	 * @param ArticlesMySql article
 	 */
	public function update($article){
		$sql = 'UPDATE articles SET article_type_id = ?, article_title = ?, article_author_id = ?, article_disclaimer_id = ?, publish_date = ?, expiry_date = ?, description = ?, status = ?, created_date = ?, updated_date = ? WHERE article_id = ?';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->setNumber($article->articleTypeId);
		$sqlQuery->set($article->articleTitle);
		$sqlQuery->setNumber($article->articleAuthorId);
		$sqlQuery->setNumber($article->articleDisclaimerId);
		$sqlQuery->set($article->publishDate);
		$sqlQuery->set($article->expiryDate);
		$sqlQuery->set($article->description);
		$sqlQuery->setNumber($article->status);
		$sqlQuery->set($article->created_date);
		$sqlQuery->set($article->updated_date);

		$sqlQuery->setNumber($article->articleId);
		return $this->executeUpdate($sqlQuery);
	}

	/**
 	 * Delete all rows
 	 */
	public function clean(){
		$sql = 'DELETE FROM articles';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}

	public function queryByArticleTypeId($value){
		$sql = 'SELECT * FROM articles WHERE article_type_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByArticleTitle($value){
		$sql = 'SELECT * FROM articles WHERE article_title = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByArticleAuthorId($value){
		$sql = 'SELECT * FROM articles WHERE article_author_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByArticleDisclaimerId($value){
		$sql = 'SELECT * FROM articles WHERE article_disclaimer_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByPublishDate($value){
		$sql = 'SELECT * FROM articles WHERE publish_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByExpiryDate($value){
		$sql = 'SELECT * FROM articles WHERE expiry_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByDescription($value){
		$sql = 'SELECT * FROM articles WHERE description = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByStatus($value){
		$sql = 'SELECT * FROM articles WHERE status = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByCreatedDate($value){
		$sql = 'SELECT * FROM articles WHERE created_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByUpdatedDate($value){
		$sql = 'SELECT * FROM articles WHERE updated_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}


	public function deleteByArticleTypeId($value){
		$sql = 'DELETE FROM articles WHERE article_type_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByArticleTitle($value){
		$sql = 'DELETE FROM articles WHERE article_title = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByArticleAuthorId($value){
		$sql = 'DELETE FROM articles WHERE article_author_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByArticleDisclaimerId($value){
		$sql = 'DELETE FROM articles WHERE article_disclaimer_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByPublishDate($value){
		$sql = 'DELETE FROM articles WHERE publish_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByExpiryDate($value){
		$sql = 'DELETE FROM articles WHERE expiry_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByDescription($value){
		$sql = 'DELETE FROM articles WHERE description = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByStatus($value){
		$sql = 'DELETE FROM articles WHERE status = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByCreatedDate($value){
		$sql = 'DELETE FROM articles WHERE created_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByUpdatedDate($value){
		$sql = 'DELETE FROM articles WHERE updated_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}


	
	/**
	 * Read row
	 *
	 * @return ArticlesMySql 
	 */
	protected function readRow($row){
		$article = new Article();
		
		$article->articleId = $row['article_id'];
		$article->articleTypeId = $row['article_type_id'];
		$article->articleTitle = $row['article_title'];
		$article->articleAuthorId = $row['article_author_id'];
		$article->articleDisclaimerId = $row['article_disclaimer_id'];
		$article->publishDate = $row['publish_date'];
		$article->expiryDate = $row['expiry_date'];
		$article->description = $row['description'];
		$article->status = $row['status'];
		$article->created_date = $row['created_date'];
		$article->updated_date = $row['updated_date'];

		return $article;
	}
	
	protected function getList($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}
	
	/**
	 * Get row
	 *
	 * @return ArticlesMySql 
	 */
	protected function getRow($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		if(count($tab)==0){
			return null;
		}
		return $this->readRow($tab[0]);		
	}
	
	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery){
		return QueryExecutor::execute($sqlQuery);
	}
	
		
	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery){
		return QueryExecutor::executeUpdate($sqlQuery);
	}

	/**
	 * Query for one row and one column
	 */
	protected function querySingleResult($sqlQuery){
		return QueryExecutor::queryForString($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery){
		return QueryExecutor::executeInsert($sqlQuery);
	}
}
?>